<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$merchid=$this->model('merch')->checklogin();
$return['steps']=array(
	array('title'=>'结算消息','en_title'=>'Shareholders'),
	array('title'=>'创建结算单','en_title'=>'Team info'),
);
if($_RQ['op']=='edit'){
	$return['step']=1;
	$item['ordermoney']=pdo_getsum('jy_liangoods_sendlog',array('status'=>4,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>0),'price');
	$item['costprice_money']=pdo_getsum('jy_liangoods_sendlog',array('clearstatus'=>0,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>4),'costprice');
	$item['balancemoney']=pdo_getsum('jy_liangoods_balance',array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'money');
	if($item['balancemoney']>0){
		$item['ordermoney'] +=$item['balancemoney'];
	}
	$sendlogs=pdo_getall('jy_liangoods_sendlog',array('status'=>4,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>0),array('id','goods'));
	$goodsnum=0;
	foreach($sendlogs as $sv){
		$goods=json_decode($sv['goods'],true);
		foreach($goods as $gv){
			$goodsnum +=$gv['total'];
		}
	}
	$salsecount=pdo_count('jy_liangoods_sendlog',array('status'=>4,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>0),'id');
	$item['balancesalsecount']=pdo_getsum('jy_liangoods_balance',array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'salsecount');
	if($item['balancesalsecount']>0){
		$salsecount +=$item['balancesalsecount'];
	}
	if(!$salsecount){
		exi('没有可结算订单','error');
	}
	$get_choufee=$this->model('merch')->get_choufee($item['ordermoney'],$merchid,1,$item['costprice_money']);
	$sys_choufee=$get_choufee['commission_money'];
	$item['starttime']=$firstday;
	$item['endtime']=$lastday;
	$item['salsecount']=$salsecount;
	$item['goodsnum']=$goodsnum;
	$item['ordermoney']=intval($item['ordermoney']*100)/100;
	$item['price']=$item['ordermoney'];
	$setting=plugin('jy_merch')->plugin_setting('setting');
	$item['feerate']=intval($setting['feerate']);
	$item['pay_type']='credit';
	$item['ratefee']=$item['price']*$item['feerate']/100;
	$item['realpricerate']=$item['price']-$item['ratefee'];
	if($setting['educt_commission']==1){
		$item['realpricerate']=$item['realpricerate']-$item['commission_money'];
		$item['educt_commission']=1;
	}
	$item['finalprice']=$item['realpricerate'];		
	//获取计算的抽成
	$item['sys_choufee']=$sys_choufee;
	$item['finalprice']=$item['finalprice']-$item['sys_choufee'];
	$item['finalprice']=$item['finalprice']<=0?0:$item['finalprice'];
	$return['pay_types']=$this->pay_types;
	$item=$item;
	$item['ratefee']=price($item['ratefee']);
	$item['sys_choufee']=price($item['sys_choufee']);
	$item['finalprice2']=$item['finalprice']+$item['ratefee'];
	$item['finalprice2']=intval($item['finalprice2']*100)/100;
	$item['finalprice']=intval($item['finalprice']*100)/100;
	if($item['finalprice'] && $this->setting['tixian_tobi']){
		$item['jifen']=$item['finalprice']/$this->setting['tixian_tobi'];
		$item['jifen']=intval($item['jifen']*100)/100;
	}
	$return['item']=$item;
	$htmls=array(
				'type'=>'edit',
				'post'=>'step2',
				'edit'=>array(
					array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_types'),
					array('type'=>'string','name'=>'realname','title'=>'真实姓名','show'=>array('pay_type'=>array('alipay','union'))),
					array('type'=>'string','name'=>'alipay','title'=>'支付宝帐号','show'=>array('pay_type'=>array('alipay'))),
					array('type'=>'string','name'=>'alipay1','title'=>'确认支付宝帐号','show'=>array('pay_type'=>array('alipay'))),
					array('type'=>'string','name'=>'bankname','title'=>'所属银行','show'=>array('pay_type'=>array('union'))),
					array('type'=>'string','name'=>'bankcard','title'=>'银行卡号','show'=>array('pay_type'=>array('union'))),
					array('type'=>'string','name'=>'bankcard1','title'=>'确认银行卡号','show'=>array('pay_type'=>array('union'))),
					array('name'=>'salsecount','title'=>'订单数量','type'=>'unitstring','unit'=>'单','read'=>true),
					array('name'=>'ordermoney','title'=>'订单金额','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'price','title'=>'提现金额','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'feerate','title'=>'提现费率','type'=>'unitstring','read'=>true,'unit'=>'点','help'=>'手续费百分比','show'=>array('pay_type'=>array('wechat','alipay','union'))),
					array('name'=>'ratefee','title'=>'提现手续费','type'=>'unitstring','unit'=>'元','read'=>true,'show'=>array('pay_type'=>array('wechat','alipay','union'))),
					//array('name'=>'sys_choufee','title'=>'平台抽成','type'=>'unitstring','unit'=>'元','read'=>true,'help'=>'不同类型商户抽成比例不一样！'),
					array('name'=>'finalprice','title'=>'到账金额','type'=>'unitstring','unit'=>'元','read'=>true,'show'=>array('pay_type'=>array('wechat','alipay','union'))),
					array('name'=>'finalprice2','title'=>'到账金额','type'=>'unitstring','unit'=>'元','read'=>true,'show'=>array('pay_type'=>array('credit','downline'))),
					array('name'=>'jifen','title'=>'赠送积分','type'=>'unitstring','unit'=>'个','read'=>true),
					array('type'=>'text','title'=>'申请事由','name'=>'content'),
				)
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('merch/apply_add');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
	
}
if($_RQ['op']=='step2'){
	$return['step']=2;
	$postdata=get_postdata();
	if($postdata['pay_type']=='alipay'){
			if(!$postdata['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$postdata['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$postdata['alipay1']){
				exi('请确认帐号','error');
			}
			if($postdata['alipay1']!=$postdata['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
	}
	if($postdata['pay_type']=='union'){
			if(!$postdata['realname']){
				exi('请输入姓名','error');
			}
			if(!$postdata['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$postdata['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$postdata['bankcard1']){
				exi('请确认卡号','error');
			}
			if($postdata['bankcard']!=$postdata['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
	}
	if($postdata['pay_type']=='credit' || $postdata['pay_type']=='downline'){
		$postdata['ratefee']=0;
		$postdata['finalprice']=$postdata['finalprice2'];
	}
	$data=array(
		'balancesn'=>get_ordersn('jy_liangoods_balance','balancesn'),
		'salsecount'=>$postdata['salsecount'],
		'money'=>$postdata['ordermoney'],
		'ratefee'=>$postdata['ratefee'],
		'balance_pay'=>$postdata['sys_choufee'],
		'final_money'=>$postdata['finalprice'],
		'pay_type'=>$postdata['pay_type'],
		'pay_trade_no'=>$postdata['pay_trade_no'],
		'realname'=>$postdata['realname'],
		'alipay'=>$postdata['alipay'],
		'bankname'=>$postdata['bankname'],
		'bankcard'=>$postdata['bankcard'],
		'status'=>0,
	);
	$data['uniacid']=$_SESSION['uniacid'];
	$data['merchid']=$merchid;
	$data['createtime']=time();
	pdo_insert('jy_liangoods_balance',$data);
	$id=pdo_insertid();
	$this->model('merch')->cash_sendlog($merchid,$postdata['finalprice'],$postdata['pay_type']);
	exi('创建结算单成功','','/merch.apply_list/status1/');
}